package com.xiyuan.smartutils.logging;

import com.xiyuan.smartutils.logging.exception.UnsupportedLevelException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * the description for class Level
 *
 * @author xiyuan-lgz 2022-10-19 @version v1.0.0 新建与整理
 */
public enum Level {
    ALL("all", 0, java.util.logging.Level.ALL),
    DEBUG("debug", 100, java.util.logging.Level.FINE),
    INFO("info", 200, java.util.logging.Level.INFO),
    WARN("warn", 300, java.util.logging.Level.WARNING),
    ERROR("error", 400, java.util.logging.Level.SEVERE),
    OFF("off", 9999, java.util.logging.Level.OFF);
    
    private int val;
    private String name;
    private java.util.logging.Level jlevel;
    
    Level(String name, int value, java.util.logging.Level jlevel) {
        this.val = value;
        this.name = name;
        this.jlevel = jlevel;
    }
    
    @Override
    public String toString() {
        return this.name;
    }
    
    protected int val() {
        return this.val;
    }
    
    protected java.util.logging.Level jleval() {
        return this.jlevel;
    }
    
    private static Map<String, Level> levelMap = new HashMap<>();
    
    static {
        Arrays.stream(Level.values()).forEach(k -> levelMap.put(k.name, k));
    }
    
    public static boolean isSupport(String name) {
        return levelMap.containsKey(name);
    }
    
    public static Level of(String name) throws UnsupportedLevelException {
        
        Level level = levelMap.get(name);
        if (level == null) {
            throw new UnsupportedLevelException("unsupported leve:" + name);
        }
        return level;
    }
}
